<template>
  <div>
    <head-top title="订单"></head-top>
    <div class="container">
      <van-contact-card
        :type="contactType"
        :name="currentContact.linkMan"
        :tel="currentContact.mobile"
        @click="choseContact"
      />
      <div>
        <van-card
          v-for="item in items"
          :key="item.id"
          :num="item.number"
          :price="item.price"
          :title="item.name"
          :thumb="item.pic"
        />
      </div>
          <van-popup v-model="show"><img class="zf" src="../../assets/img/zf.jpg"> </van-popup>
      <van-submit-bar :price="allprice * 100" button-text="扫码支付" @submit="show = true"> </van-submit-bar>
    </div>
  </div>
</template>

<script>
import HeadTop from '../../components/headtop.vue'
import { getToken } from '_utils/token.js'
import { getContacts, getContactmsg, cartList } from '_api'
export default {
  components: { HeadTop },
  data () {
    return {
      contactType: 'add',
      currentContact: {},
      items: [],
      allprice: '',
      show: false
    }
  },
  created () {
    this.cartList()
    this.getContact()
  },
  methods: {
    choseContact () {
      if (this.contactType === 'add') {
        this.$router.push('/contactsAdd')
      } else {
        this.$router.push('/contactsList')
      }
    },
    cartList () {
      cartList({
        token: getToken()
      }).then((res) => {
        if (res.data.code === 0) {
          this.allprice = res.data.data.price
          this.items = res.data.data.items.filter((item) => item.selected)
        }
      })
    },
    getContact () {
      if (this.$route.query.contactId) {
        getContactmsg({
          id: this.$route.query.contactId,
          token: getToken()
        }).then((res) => {
          if (res.data.code === 0) {
            this.currentContact = res.data.data.info
            this.contactType = 'edit'
          }
        })
        return
      }
      getContacts({
        token: getToken()
      }).then((res) => {
        if (res.data.code === 0) {
          this.contactType = 'edit'
          const contacts = res.data.data.result
          const defaultContact = contacts.find((contact) => contact.isDefault)
          if (defaultContact) {
            this.currentContact = defaultContact
          } else {
            this.currentContact = contacts[0]
          }
        }
      })
    },
    gofire () {

    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  padding-top: 0.9rem;
  padding-bottom: 1.1rem;
  .van-submit-bar {
    border-top: 1px solid rgb(190, 189, 189);
  }
  .zf {width: 6rem;display: block;margin: 0 auto;}
}
</style>
